//
//  AddressViewController.m
//  chongwu
//
//  Created by 隼目 on 2021/9/16.
//

#import "AddressViewController.h"
#import "AddressTableViewCell.h"
@interface AddressViewController ()<UITableViewDelegate,UITableViewDataSource,addressDelegate>
@property(nonatomic,strong)BaseTableView *tableView;

@property(nonatomic,assign)BOOL isAgreement;
@property(nonatomic,strong)NSString *nameStr;
@property(nonatomic,strong)NSString *phoneStr;
@property(nonatomic,strong)NSString *cityStr;
@property(nonatomic,strong)NSString *detailedStr;

@end

@implementation AddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.typeIndex==1) {
        self.navigationItem.title=@"寄件人信息填写";
    }else if (self.typeIndex==2) {
        self.navigationItem.title=@"收件人信息填写";
    }
    AddressModel *model=[AddressModel mj_objectWithKeyValues:self.addressDic];
    
    self.nameStr=model.addressUsername;
    self.phoneStr=model.addressPhone;
    self.detailedStr=model.addressDetail;
    self.cityStr=[NSString stringWithFormat:@"%@%@%@",model.addressProvincial?model.addressProvincial:@"",model.addressCity?model.addressCity:@"",model.addressArea?model.addressArea:@""];
    self.isAgreement=model.defaultAddress;
    
    self.tableView=[BaseTableView new];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(0);
    }];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor=rgba(245, 246, 246, 1);
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
}
-(void)viewWillAppear:(BOOL)animated{
    [[CDPMonitorKeyboard defaultMonitorKeyboard] sendValueWithSuperView:self.tableView higherThanKeyboard:0 andMode:CDPMonitorKeyboardDefaultMode];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AddressTableViewCell *cell=[[AddressTableViewCell alloc] init];
    cell.delegate=self;
    cell.nameStr=self.nameStr;
    cell.phoneStr=self.phoneStr;
    cell.detailedStr=self.detailedStr;
    cell.cityStr=self.cityStr;
    cell.isAgreement=self.isAgreement;
    if (indexPath.row==0) {
        [cell creatView];
    }else if (indexPath.row==1) {
        [cell creatBtnView];
    }
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        return  675*_mainHeight;
    }
    return 167*_mainHeight;

}
-(void)textFiledDidEnd:(UITextField *)text{
   
    NSString *testStr=text.text;
    if (text.tag==1) {
        self.nameStr=testStr;
    }else if (text.tag==2) {
        self.phoneStr=testStr;
    }
}
-(void)textViewdDidEnd:(UITextView *)text{
    if (text.tag==4) {
        self.detailedStr=text.text;
    }
}
-(void)selectAgreement{
    [self.view endEditing:YES];
    
    if (self.isAgreement) {
        self.isAgreement=NO;
    }else{
        self.isAgreement=YES;
    }
    [self.tableView reloadData];
}
-(void)selectEmpty{
    [self.view endEditing:YES];
    
    self.isAgreement=NO;
    self.nameStr=nil;
    self.phoneStr=nil;
    self.cityStr=nil;
    self.detailedStr=nil;
    [self.tableView reloadData];
}
-(void)selectSubmit{
    [self.view endEditing:YES];
    
    if ([ZQTools charIsNil:self.nameStr]) {
        if (self.typeIndex==1) {
            [ZQTools svpInfo:@"请输入寄件人姓名"];
        }else if (self.typeIndex==2) {
            [ZQTools svpInfo:@"请输入收件人姓名"];
        }
        return;
    }if ([ZQTools charIsNil:self.phoneStr]) {
        if (self.typeIndex==1) {
            [ZQTools svpInfo:@"请输入寄件人联系电话"];
        }else if (self.typeIndex==2) {
            [ZQTools svpInfo:@"请输入收件人联系电话"];
        }
        return;
    }if ([ZQTools charIsNil:self.detailedStr]) {
        if (self.typeIndex==1) {
            [ZQTools svpInfo:@"请输入寄件人详细地址"];
        }else if (self.typeIndex==2) {
            [ZQTools svpInfo:@"请输入收件人详细地址"];
        }
        return;
    }
    NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithCapacity:0];
    [dic setValue:self.nameStr forKey:@"addressUsername"];
    [dic setValue:self.phoneStr forKey:@"addressPhone"];
    [dic setValue:self.detailedStr forKey:@"addressDetail"];
    
    if (self.addressDic) {
        AddressModel *model=[AddressModel mj_objectWithKeyValues:self.addressDic];
        [dic setValue:model.addressId forKey:@"addressId"];
        [self PutWithUrl:@"/member/address" param:dic tableView:self.tableView success:^(id  _Nullable dict) {
            [ZQTools svpInfo:@"修改成功"];
            self.addressBackBlok();
            [self popVc];
        } failure:^(NSError * _Nullable error) {
            
        }];
    }else{
        [self PostWithUrl:@"/member/address" param:dic tableView:self.tableView success:^(id  _Nullable dict) {
            [ZQTools svpInfo:@"添加成功"];
            self.addressBackBlok();
            [self popVc];
            
        } failure:^(NSError * _Nullable error) {
            
        }];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
